﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using BGSpaceShooter;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;


namespace BGSpaceShooterServer
{
    internal class Player
    {
        public IPAddress PlayerAddress { get; set; }
        public int PlayerPort { get; set; }
        public int PlayerID { get; set; }

        public bool Active { get; set; }
        public UserType PlayerType { get; set; }
        public DateTime LastUpdate { get; set; }
        public DateTime LastRequest { get; set; }

        private List<Keys> lastInput;
        private DateTime lastInputUpdate;

        public DateTime LastProjectileCreation { get; set; }
        public List<Keys> LastInput
        {
            get
            {
                lock (lastInput)
                {
                    return lastInput;
                }
            }
        }

        public void SetLastInput(List<Keys> input, DateTime requestTime)
        {
            lock (lastInput)
            {
                if (DateTime.Compare(requestTime, lastInputUpdate) > 0)
                {
                    lastInputUpdate = requestTime;
                    lastInput = input;
                }
            }
        }

        public int Points { get; set; }

        public Player()
        {
            LastUpdate = DateTime.Now;
            LastRequest = DateTime.Now;
            lastInput = new List<Keys>();
            lastInputUpdate = DateTime.Now;
            LastProjectileCreation = DateTime.Now;
        }

        internal StarFighter StarFighter
        {
            get;
            set;
        }

        public ServerStatusResponse GetStatusResponse()
        {
            ServerStatusResponse response = StarFighter.GetStatusResponse();
            response.playerId = PlayerID;
            return response;
        }

        public IEnumerable<ServerStatusResponse> GetHPUpdates()
        {
            List<ServerStatusResponse> updates = new List<ServerStatusResponse>();

            if (StarFighter.HPChanged)
            {
                updates.Add(new ServerStatusResponse() { playerId = PlayerID, ComponentIndex = -1, HPValue = StarFighter.HP, TimeStamp = DateTime.Now });
                StarFighter.HPChanged = false;
            }

            for (int i = 0; i < StarFighter.StarFighterComponents.Count; ++i)
            {
                if (StarFighter.StarFighterComponents[i].HPChanged)
                {
                    updates.Add(new ServerStatusResponse()
                                    {
                                        playerId = PlayerID,
                                        ComponentIndex = i,
                                        HPValue = StarFighter.StarFighterComponents[i].HP,
                                        TimeStamp = DateTime.Now
                                    });
                    StarFighter.StarFighterComponents[i].HPChanged = false;
                }
            }

            return updates;
        }

        public void Reset()
        {
            Random rand = new Random();
            StarFighter.Position = new Vector3(rand.Next(200) - rand.Next(200), rand.Next(200) - rand.Next(200), rand.Next(200) - rand.Next(200));
            StarFighter.Rotation *= Matrix.CreateRotationX((float)rand.NextDouble()) * Matrix.CreateRotationY((float)rand.NextDouble()) * Matrix.CreateRotationZ((float)rand.NextDouble());
            StarFighter.Velocity = new Vector3(0);
            StarFighter.RotationVel = new Vector3(0);
            StarFighter.HP = StarFighter.MaxHP;
            StarFighter.LastPlayerIdHit = -1;
            foreach (var starFighterComponent in StarFighter.StarFighterComponents)
            {
                starFighterComponent.HP = starFighterComponent.MaxHP;
                starFighterComponent.HPChanged = true;
            }

        }
    }
}
